package com.anniljing.kotlionlearn.Basic.func

fun main() {
    val items = listOf(1, 2, 3, 4, 5)
    println(items.fold(0, { acc: Int, i: Int -> acc + i }))
    println(items.count { i: Int -> i % 2 == 0 })
    println(items.find { it == 3 })
    println(items.findLast { it % 2 == 1 })
    println(addWithOtherTwo(8, 9, 10) { x: Int, y: Int -> if (x > y) x else y })
    addWithMax(5,6,7)
    println(getSum()(1, 2))
    println(handleTwoParameters(5,6)(8, 9))
}


/**
 * lambda
 */
val test: (Int, Int) -> Int = { a, b -> a + b }

/**
 *A higher-order function is a function that takes functions as parameters
 */
fun addWithMax(
    base: Int,
    a: Int,
    b: Int,
    max: (Int, Int) -> Int = { x, y -> if (x > y) x else y }
): Int {
    return base + max(a, b)
}

/**
 *A higher-order function is a function that takes functions as parameters
 */
fun addWithOtherTwo(base: Int, x: Int, y: Int, max: (Int, Int) -> Int): Int {
    return base + max(x, y)
}

/**
 * A higher-order function is a function that takes functions as returns a function
 */
fun handleTwoParameters(a: Int,b: Int):(Int,Int) ->Int{
    return plusTwo
}

val plusTwo:(Int,Int)->Int={x,y->x+y}

fun getSum(): (Int, Int) -> Int {

    return { a, b -> a + b }
}



